package com.hopu.springmvc.controller;

import com.hopu.springmvc.entity.User;
import com.hopu.springmvc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 */
@Controller
//会给类中所有方法的url前加/user/
@RequestMapping("user")
public class UserController {

    //注入Service对象
    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(String username, String password, Model model, HttpSession session){
        User user = userService.login(username, password);
        if(user == null){
            model.addAttribute("error","账号或密码错误");
            return "login";
        }
        //把User存入Session
        session.setAttribute("user",user);
        return "redirect:list";
    }

    @RequestMapping("list")
    public String list(Model model){
        List<User> users = userService.queryAllUsers();
        model.addAttribute("users",users);
        return "list";
    }

    @RequestMapping("add")
    //User参数用于包装除文件域外的参数，@RequestParam配置文件域，required = false代表非必填
    public String add(User user, @RequestParam(value = "pic",required = false) CommonsMultipartFile file){
        if(file != null){
            //获得原来的文件名
            String filename = file.getOriginalFilename();
            //截取后缀名
            String suffix = filename.substring(filename.lastIndexOf("."));
            //使用当前时间创建新文件名
            filename = System.currentTimeMillis()+suffix;
            File uploadFile = new File("D:\\upload\\"+filename);
            //保存文件名到对象属性中
            user.setPicture(filename);
            //上传文件
            try {
                file.transferTo(uploadFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //保存数据库
        System.out.println("保存到数据库：" + user);
        return "list";
    }

//    /**
//     * 执行登录操作
//     * @return
//     */
//    @RequestMapping("login1")
//    public String login1(HttpServletRequest request){
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        System.out.println(username+"---"+password);
//        return "login";
//    }
//
//    @RequestMapping("login2")
//    public String login2(String username,String password){
//        System.out.println(username+"****"+password);
//        return "login";
//    }
//
//    @RequestMapping("login3")
//    public String login3(@RequestParam("username") String user,
//                         @RequestParam("password") String pass){
//        System.out.println(user+"!!!!"+pass);
//        return "login";
//    }
//
//    @RequestMapping("login4")
//    public String login4(User user){
//        System.out.println(user);
//        //请求转发
////        return "forward:list3";
//        //重定向
//        return "redirect:list3";
//    }
//
//    private List<User> users = new ArrayList<>();
//    {
//        users.add(new User("zhangsan","123","张三",20));
//        users.add(new User("lisi","123","李四",20));
//        users.add(new User("wangwu","123","王五",20));
//        users.add(new User("zhaoliu","123","赵六",20));
//        users.add(new User("zhouqi","123","周七",20));
//    }
//
//    @RequestMapping("list1")
//    public String list1(HttpServletRequest request){
//        request.setAttribute("users",users);
//        return "list";
//    }
//
//    @RequestMapping("list2")
//    public String list2(Map map){
//        map.put("users",users);
//        return "list";
//    }
//
//    @RequestMapping("list3")
//    public String list3(Model model){
//        model.addAttribute("users",users);
//        return "list";
//    }
}
